﻿using HutongGames.PlayMaker;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using AddOn;

[AddComponentMenu("PlayMaker/PlayMakerFSM")]
public class PlayMakerFSM : MonoBehaviour, IBlock
{
    [CompilerGenerated]
    private static bool DrawGizmos__BackingField;
    public static bool ApplicationIsQuitting;
    [SerializeField, HideInInspector]
    private HutongGames.PlayMaker.Fsm fsm;
    private static readonly List<PlayMakerFSM> fsmList = new List<PlayMakerFSM>();

    private void AddToFsmList()
    {
        if (!fsmList.Contains(this))
        {
            fsmList.Add(this);
        }
    }

    private void Awake()
    {
        this.AddToFsmList();
    }

    public static void BroadcastEvent(FsmEvent fsmEvent)
    {
        List<PlayMakerFSM> list = new List<PlayMakerFSM>(FsmList);
        foreach (PlayMakerFSM rfsm in list)
        {
            if ((rfsm != null) && (rfsm.Fsm != null))
            {
                rfsm.Fsm.ChangeState(fsmEvent);
            }
        }
    }

    public static void BroadcastEvent(string fsmEventName)
    {
        if (!string.IsNullOrEmpty(fsmEventName))
        {
            BroadcastEvent(FsmEvent.GetFsmEvent(fsmEventName));
        }
    }

    public void ChangeState(FsmEvent fsmEvent)
    {
        this.fsm.Event(fsmEvent);
    }

    [Obsolete("Use SendEvent(string) instead.")]
    public void ChangeState(string eventName)
    {
        this.fsm.Event(eventName);
    }

    private void FixedUpdate()
    {
        this.fsm.FixedUpdate();
    }

    private void LateUpdate()
    {
        this.fsm.LateUpdate();
    }

    private void OnApplicationQuit()
    {
        ApplicationIsQuitting = true;
    }

    private void OnBecameInvisible()
    {
        this.fsm.Event(FsmEvent.BecameInvisible);
    }

    private void OnBecameVisible()
    {
        this.fsm.Event(FsmEvent.BecameVisible);
    }

    private void OnCollisionEnter(Collision collisionInfo)
    {
        this.fsm.OnCollisionEnter(collisionInfo);
    }

    private void OnCollisionExit(Collision collisionInfo)
    {
        this.fsm.OnCollisionExit(collisionInfo);
    }

    private void OnCollisionStay(Collision collisionInfo)
    {
        this.fsm.OnCollisionStay(collisionInfo);
    }

    private void OnConnectedToServer()
    {
        this.fsm.Event(FsmEvent.ConnectedToServer);
    }

    private void OnControllerColliderHit(ControllerColliderHit hitCollider)
    {
        this.fsm.OnControllerColliderHit(hitCollider);
    }

    private void OnDestroy()
    {
        this.fsm.OnDestroy();
    }

    private void OnDisable()
    {
        this.RemoveFromFsmList();
        this.fsm.OnDisable();
    }

    private void OnDisconnectedFromServer(NetworkDisconnection info)
    {
        HutongGames.PlayMaker.Fsm.EventData.DisconnectionInfo = info;
        this.fsm.Event(FsmEvent.DisconnectedFromServer);
    }

    private void OnDrawGizmos()
    {
        this.fsm.OnDrawGizmos();
    }

    private void OnEnable()
    {
        this.AddToFsmList();
        this.fsm.OnEnable();
    }

    private void OnFailedToConnect(NetworkConnectionError error)
    {
        HutongGames.PlayMaker.Fsm.EventData.ConnectionError = error;
        this.fsm.Event(FsmEvent.FailedToConnect);
    }

    private void OnLevelWasLoaded()
    {
        this.fsm.Event(FsmEvent.LevelLoaded);
    }

    private void OnMasterServerEvent(MasterServerEvent masterServerEvent)
    {
        HutongGames.PlayMaker.Fsm.EventData.MasterServerEvent = masterServerEvent;
        this.fsm.Event(FsmEvent.MasterServerEvent);
    }

    private void OnMouseDown()
    {
        if (Application.isPlaying)
        {
            this.fsm.Event(FsmEvent.MouseDown);
        }
    }

    private void OnMouseDrag()
    {
        if (Application.isPlaying)
        {
            this.fsm.Event(FsmEvent.MouseDrag);
        }
    }

    private void OnMouseEnter()
    {
        if (Application.isPlaying)
        {
            this.fsm.Event(FsmEvent.MouseEnter);
        }
    }

    private void OnMouseExit()
    {
        if (Application.isPlaying)
        {
            this.fsm.Event(FsmEvent.MouseExit);
        }
    }

    private void OnMouseOver()
    {
        if (Application.isPlaying)
        {
            this.fsm.Event(FsmEvent.MouseOver);
        }
    }

    private void OnMouseUp()
    {
        if (Application.isPlaying)
        {
            this.fsm.Event(FsmEvent.MouseUp);
            HutongGames.PlayMaker.Fsm.LastClickedObject = base.gameObject;
        }
    }

    private void OnNetworkInstantiate(NetworkMessageInfo info)
    {
        HutongGames.PlayMaker.Fsm.EventData.NetworkMessageInfo = info;
        this.fsm.Event(FsmEvent.NetworkInstantiate);
    }

    private void OnPlayerConnected(NetworkPlayer player)
    {
        HutongGames.PlayMaker.Fsm.EventData.Player = player;
        this.fsm.Event(FsmEvent.PlayerConnected);
    }

    private void OnPlayerDisconnected(NetworkPlayer player)
    {
        HutongGames.PlayMaker.Fsm.EventData.Player = player;
        this.fsm.Event(FsmEvent.PlayerDisconnected);
    }

    private void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
    {
        if (stream.isWriting)
        {
            foreach (FsmBool @bool in this.Fsm.Variables.BoolVariables)
            {
                if (@bool.NetworkSync)
                {
                    bool flag = @bool.Value;
                    stream.Serialize(ref flag);
                }
            }
            foreach (FsmFloat num in this.Fsm.Variables.FloatVariables)
            {
                if (num.NetworkSync)
                {
                    float num2 = num.Value;
                    stream.Serialize(ref num2);
                }
            }
            foreach (FsmInt num3 in this.Fsm.Variables.IntVariables)
            {
                if (num3.NetworkSync)
                {
                    int num4 = num3.Value;
                    stream.Serialize(ref num4);
                }
            }
            foreach (FsmQuaternion quaternion in this.Fsm.Variables.QuaternionVariables)
            {
                if (quaternion.NetworkSync)
                {
                    Quaternion quaternion2 = quaternion.Value;
                    stream.Serialize(ref quaternion2);
                }
            }
            foreach (FsmVector3 vector in this.Fsm.Variables.Vector3Variables)
            {
                if (vector.NetworkSync)
                {
                    Vector3 vector2 = vector.Value;
                    stream.Serialize(ref vector2);
                }
            }
        }
        else
        {
            foreach (FsmBool bool2 in this.Fsm.Variables.BoolVariables)
            {
                if (bool2.NetworkSync)
                {
                    bool flag2 = false;
                    stream.Serialize(ref flag2);
                    bool2.Value = flag2;
                }
            }
            foreach (FsmFloat num5 in this.Fsm.Variables.FloatVariables)
            {
                if (num5.NetworkSync)
                {
                    float num6 = 0f;
                    stream.Serialize(ref num6);
                    num5.Value = num6;
                }
            }
            foreach (FsmInt num7 in this.Fsm.Variables.IntVariables)
            {
                if (num7.NetworkSync)
                {
                    int num8 = 0;
                    stream.Serialize(ref num8);
                    num7.Value = num8;
                }
            }
            foreach (FsmQuaternion quaternion3 in this.Fsm.Variables.QuaternionVariables)
            {
                if (quaternion3.NetworkSync)
                {
                    Quaternion identity = Quaternion.identity;
                    stream.Serialize(ref identity);
                    quaternion3.Value = identity;
                }
            }
            foreach (FsmVector3 vector3 in this.Fsm.Variables.Vector3Variables)
            {
                if (vector3.NetworkSync)
                {
                    Vector3 zero = Vector3.zero;
                    stream.Serialize(ref zero);
                    vector3.Value = zero;
                }
            }
        }
    }

    private void OnServerInitialized()
    {
        this.fsm.Event(FsmEvent.ServerInitialized);
    }

    private void OnTriggerEnter(Collider other)
    {
        this.fsm.OnTriggerEnter(other);
    }

    private void OnTriggerExit(Collider other)
    {
        this.fsm.OnTriggerExit(other);
    }

    private void OnTriggerStay(Collider other)
    {
        this.fsm.OnTriggerStay(other);
    }

    private void RemoveFromFsmList()
    {
        fsmList.Remove(this);
    }

    private void Reset()
    {
        if (this.fsm == null)
        {
            this.fsm = new HutongGames.PlayMaker.Fsm();
        }
        this.fsm.Reset(this);
    }

    public void SendEvent(string eventName)
    {
        this.fsm.Event(eventName);
    }

    private void Start()
    {
        if (this.fsm == null)
        {
            this.Reset();
        }
        if (this.fsm != null)
        {
            this.fsm.Init(this);
            this.fsm.EnterStartState();
        }
    }

    private void Update()
    {
        this.fsm.Update();
    }

    public bool Active
    {
        get
        {
            return this.fsm.Active;
        }
    }

    public string ActiveStateName
    {
        get
        {
            if (this.fsm.ActiveState != null)
            {
                return this.fsm.ActiveState.Name;
            }
            return "";
        }
    }

    public static bool DrawGizmos
    {
        [CompilerGenerated]
        get
        {
            return DrawGizmos__BackingField;
        }
        [CompilerGenerated]
        set
        {
            DrawGizmos__BackingField = value;
        }
    }

    public HutongGames.PlayMaker.Fsm Fsm
    {
        get
        {
            return this.fsm;
        }
    }

    public string FsmDescription
    {
        get
        {
            return this.fsm.Description;
        }
        set
        {
            this.fsm.Description = value;
        }
    }

    public FsmEvent[] FsmEvents
    {
        get
        {
            return this.fsm.Events;
        }
    }

    public FsmTransition[] FsmGlobalTransitions
    {
        get
        {
            return this.fsm.GlobalTransitions;
        }
    }

    public static List<PlayMakerFSM> FsmList
    {
        get
        {
            return fsmList;
        }
    }

    public string FsmName
    {
        get
        {
            return this.fsm.Name;
        }
        set
        {
            this.fsm.Name = value;
        }
    }

    public FsmState[] FsmStates
    {
        get
        {
            return this.fsm.States;
        }
    }

    public HutongGames.PlayMaker.FsmVariables FsmVariables
    {
        get
        {
            return this.fsm.Variables;
        }
    }

    [SerializeField]
    private Rect position = new Rect(0, 0, 100, 50);

    public Rect Position
    {
        get
        {
            return position;
        }
        set
        {
            position = value;
        }
    }
}

